<?php
elgg_register_event_handler('init', 'system', 'tournaments_init');

function tournaments_init()
{
	//Actions
	$action_base = elgg_get_plugins_path() . 'padelot_tournament_manager/actions/tournaments';
	elgg_register_action('tournaments/save', "$action_base/save.php");
	elgg_register_action('tournaments/delete', "$action_base/delete.php");
	
	//Page handler
	elgg_register_page_handler('tournaments', 'tournaments_page_handler');
	
	//Load libs
	elgg_register_library('elgg:tournaments', elgg_get_plugins_path()."padelot_tournament_manager/lib/tournaments.php");
	elgg_register_library('elgg:class_tournament', elgg_get_plugins_path()."padelot_tournament_manager/lib/class.Tournament.php");
	elgg_register_library('elgg:tournaments:manager', elgg_get_plugins_path()."padelot_tournament_manager/lib/manager.php");
	
	//Permissions check
	elgg_register_plugin_hook_handler('permissions_check', 'object','check_organization_permissions');
	
	//Url handlers
	elgg_register_entity_url_handler('object', 'tournament', 'tournaments_url');
	elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'tournaments_icon_url_override');
	
	// Register an icon handler for tournaments
	elgg_register_page_handler('tournament_icon', 'tournaments_icon_handler');
	
	//Register entity for search
	elgg_register_entity_type('object', 'tournament');
	add_subtype('object', 'tournament', 'Tournament');
}

function tournaments_page_handler($page)
{
	elgg_load_library('elgg:tournaments');
	elgg_load_library('elgg:class_tournament');
	
	switch($page[0])
	{
		case 'add':
			tournaments_handle_add_page($page[1]);
			break;
		case 'edit':
			tournaments_handle_edit_page($page[1]);
			break;
		case 'manager':
			
		default:
			tournaments_handle_view_page($page[1]);
	}
	
	
}

/**
 * Handle tournaments icons.
 *
 * @param unknown_type $page
 */
function tournaments_icon_handler($page) {

	// The username should be the file we're getting
	if (isset($page[0])) {
		set_input('tournament_guid', $page[0]);
	}
	if (isset($page[1])) {
		set_input('size', $page[1]);
	}
	// Include the standard profile index
	$plugin_dir = elgg_get_plugins_path();
	include("$plugin_dir/padelot_tournament_manager/icon.php");
}

function tournaments_url($entity)
{
	$title = $entity->title;
	$title = elgg_get_friendly_title($title);
	return $CONFIG->url . "tournaments/view/" . $entity->getGUID();
}

function tournaments_icon_url_override($hook, $type, $returnvalue, $params)
{
	$entity = $params['entity'];
	$size = $params['size'];
	
	if(elgg_instanceof($entity, 'object', 'tournament'))
	{
		
		$icontime = $entity->getMetaData('icontime');
		if ($icontime)
		{
			// return thumbnail
			return "tournament_icon/$entity->guid/$size/$icontime.jpg";
		}
	
		return "mod/padelot_tournament_manager/graphics/default{$size}.gif";
	}
}

function check_tournament_permissions($hook, $entity_type, $returnvalue, $params)
{
	$tournament = $params['entity'];
	$user = $params['user'];
	
	if(elgg_instanceof($tournament, 'object', 'tournament'))
	{
		return $tournament->isManager($user);
	}
	else
		return NULL;
}

?>